package com.yinhe.music.yhmusic.base;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * @author wm
 * @date 2016/3/17
 */
public abstract class BaseServiceFragment extends BaseFragment implements IBaseView {

    private View rootView;
    private Unbinder unbinder;
    private boolean isInit;


    /**
     * 获取Presenter实例，子类实现
     *
     * @return Presenter实例
     */
    public abstract BasePresenter getPresenter();

    /**
     * 初始化Presenter的实例，子类实现
     */
    public abstract void initPresenter();

    /**
     * 初始化界面
     */
    public abstract void initView();

    /**
     * 设置布局id
     *
     * @return 布局id
     */
    public abstract int getLayoutId();

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {

        if (rootView == null && getLayoutId() > 0) {
            rootView = inflater.inflate(getLayoutId(), container, false);
        } else {
            ViewGroup viewGroup = (ViewGroup) rootView.getParent();
            if (viewGroup != null) {
                viewGroup.removeView(rootView);
            }
        }
        unbinder = ButterKnife.bind(this, rootView);

        if (!isInit) {
            isInit = true;
            initPresenter();
            initView();
        }
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (isInit) unbinder.unbind();
    }

    @Override
    public void onDestroy() {
        if (getPresenter() != null) {
            getPresenter().dispose();
            getPresenter().onDetach();
        }
        super.onDestroy();
    }

    /**
     * activity连接情况
     */
    protected boolean isAttachedContext() {
        return getActivity() != null;
    }

    /**
     * 检查activity连接情况
     */
    public void checkActivityAttached() {
        if (getActivity() == null) {
            throw new ActivityNotAttachedException();
        }
    }

    /**
     * 抛出activity连接异常
     */
    private static class ActivityNotAttachedException extends RuntimeException {
        ActivityNotAttachedException() {
            super("Fragment has disconnected from Activity ! - -.");
        }
    }

    @Override
    public void showLoading() {
        checkActivityAttached();
        if (getActivity() instanceof BaseServiceActivity) {
            ((BaseServiceActivity) getActivity()).showLoading();
        }
    }

    @Override
    public void hideLoading() {
        checkActivityAttached();
        if (getActivity() instanceof BaseServiceActivity) {
            ((BaseServiceActivity) getActivity()).hideLoading();
        }
    }

    @Override
    public void showToast(String msg) {
        checkActivityAttached();
        if (getActivity() instanceof BaseServiceActivity) {
            ((BaseServiceActivity) getActivity()).showToast(msg);
        }
    }

    @Override
    public void showMessage(Throwable throwable) {
        checkActivityAttached();
        if (getActivity() instanceof BaseServiceActivity) {
            ((BaseServiceActivity) getActivity()).showMessage(throwable);
        }
    }

    @Override
    public void showPlayList() {
        checkActivityAttached();
        if (getActivity() instanceof BaseServiceActivity) {
            ((BaseServiceActivity) getActivity()).showPlayList();
        }
    }

    @Override
    public void startLogin() {
        checkActivityAttached();
        if (getActivity() instanceof BaseServiceActivity) {
            ((BaseServiceActivity) getActivity()).startLogin();
        }
    }

    @Override
    public void setLoadMoreList(List list, int pageNum, BaseQuickAdapter mAdapter) {
        checkActivityAttached();
        if (getActivity() instanceof BaseServiceActivity) {
            ((BaseServiceActivity) getActivity()).setLoadMoreList(list, pageNum, mAdapter);
        }
    }
}
